package com.school.information.utils;

import com.google.common.collect.Lists;
import com.google.common.io.Files;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;

@Slf4j
public class FileUtil {
    /**
     * 单个文件上传
     *
     * @param file
     * @param filePath
     * @return
     */
    public static String uploadSingleFilePath(MultipartFile file, String filePath) {
        String path = null;
        File localFile = null;
        if (!file.isEmpty()) {
            try {
                localFile = saveToLocal(file, filePath);
                log.info("##localFile = " + localFile);
                path = StringUtils.substringAfterLast(localFile.getAbsolutePath(), filePath);
                log.info("##path = " + path);
            } catch (Exception e) {
                throw new IllegalArgumentException(e);
            }
        }
        return path;
    }

    /**
     * 批量文件上传
     *
     * @param files
     * @param filePath
     * @return
     */
    public static List<String> uploadMultiFilePath(List<MultipartFile> files, String filePath) {
        List<String> paths = Lists.newArrayList();
        files.forEach(file -> {
            File localFile = null;
            if (!file.isEmpty()) {
                try {
                    localFile = saveToLocal(file, filePath);
                    String path = StringUtils.substringAfterLast(localFile.getAbsolutePath(), filePath);
                    paths.add(path);
                } catch (Exception e) {
                    throw new IllegalArgumentException(e);
                }
            }
        });
        return paths;
    }

    /**
     * 删除单个文件
     *
     * @param fileName 被删除的文件名
     * @return 如果删除成功，则返回true，否则返回false
     */
    public static boolean deleteFile(String fileName) {
        File file = new File(fileName);
        if (file.exists() && file.isFile()) {
            if (file.delete()) {
                log.debug("删除文件 " + fileName + " 成功!");
                return true;
            } else {
                log.debug("删除文件 " + fileName + " 失败!");
                return false;
            }
        } else {
            log.debug(fileName + " 文件不存在!");
            return true;
        }
    }

    /**
     * 保存文件
     *
     * @param file
     * @param filePath
     * @return
     * @throws IOException
     */
    private static File saveToLocal(MultipartFile file, String filePath) throws IOException {
//        String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
        long fileTime = System.currentTimeMillis();
        File newFile = new File(filePath + File.separator + fileTime + File.separator + file.getOriginalFilename());
        if (!newFile.exists()) {
            newFile.getParentFile().mkdirs();
            newFile.createNewFile();
        }
        Files.write(file.getBytes(), newFile);
        log.info("##newFile = " + newFile);
        return newFile;
    }
}
